package com.ruoyi.framework.web.service;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.JimuTokenUtils;
import org.jeecg.modules.jmreport.api.JmReportTokenServiceI;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Component
public class JimuReportTokenService implements JmReportTokenServiceI {


    @Resource
    private TokenService tokenService;

    /**
     * 自定义获取TOKEN
     *
     * @param request
     * @return
     */
    @Override
    public String getToken(HttpServletRequest request) {
        return JimuTokenUtils.getTokenByRequest(request);
    }

    /**
     * @param token
     * @return
     */
    @Override
    public String getUsername(String token) {
        LoginUser loginUser = tokenService.getLoginUser(token);
        if (ObjectUtil.isNotEmpty(loginUser)) {
            return loginUser.getUsername();
        }
        return "";
    }

    /**
     * Token校验
     *
     * @param token
     * @return
     */
    @Override
    public Boolean verifyToken(String token) {
        LoginUser loginUser = tokenService.getLoginUser(token);
        if (ObjectUtil.isNotEmpty(loginUser)) {
            tokenService.verifyToken(loginUser);
            return true;
        }
        return false;
    }


    /**
     * 积木报表暂未启用，这边不返回任何的角色
     * @param token
     * @return
     */
    @Override
    public String[] getRoles(String token) {
        return new String[]{};
    }
}